package im.starchat.chat.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import im.starchat.chat.R;
import im.starchat.chat.activity.PlayBackActivity;
import im.starchat.chat.rest.model.MemberInfo;
import im.starchat.chat.view.CircularImageButton;

/**
 * Created by admin on 15/11/7.
 */
public class MemberListAdapter extends BaseAdapter {
    private static final String TAG = MemberListAdapter.class.getSimpleName();

    Context context;
    List<MemberInfo> list;

    public MemberListAdapter(Context context, MemberListCallBack memberListCallBack) {
        this.mMemberListCallBack = memberListCallBack;
        this.context = context;
    }

    public MemberListAdapter(Context context, List<MemberInfo> list) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        if(list == null){
            return 0;
        }
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Log.d(TAG, "Adapter getView  " + position);
            if (convertView == null) {
                LayoutInflater layoutInflater = LayoutInflater.from(context);
                convertView = layoutInflater.inflate(R.layout.layout_member_item, null);
            }
            CircularImageButton headImage = (CircularImageButton) convertView.findViewById(R.id.head_image);
            headImage.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            final MemberInfo item = list.get(position);
//        headImage.setImageBitmap(item.getHeadImage());
            if (!TextUtils.isEmpty(item.getHeadImagePath())) {
                Picasso.with(context).load(item.getHeadImagePath())
                        .placeholder(R.drawable.home_bg)
                        .resize(100, 100)
                        .into(headImage);
            }

            headImage.setClickable(false);
            headImage.setFocusable(false);
            headImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mMemberListCallBack != null) {
                        mMemberListCallBack.onMemberClick(item);
                    }
                }
            });
//        TextView textview =(TextView) convertView.findViewById(R.id.member_name);
//        textview.setText(""+item.getUserName());
            if(position == list.size()-1) {
                if (context instanceof PlayBackActivity) {
                    ((PlayBackActivity) context).loadmore();
                }
            }
            return convertView;
    }

    public void refreshMemberData(List<MemberInfo> list){
        this.list=list;
        notifyDataSetChanged();

    }

    private MemberListCallBack mMemberListCallBack;
    public static interface MemberListCallBack{
        public void onMemberClick(MemberInfo memberInfo);
    }

}
